clear all;
close all;

r4r6_data_raw=importdata('g:\Research_Data\talinr912\goodunfolding.txt');
save_folder='G:\Mingxi\Dropbox\Mingxi-files\FL talin\figures\mechanical responses\';
transition_no=4;
no_repeats=10;
parsed_unfolding_data_slow=[];
parsed_unfolding_data_fast=[];
%r4r6_data=[r4r6_data_raw1.data;r4r6_data_raw2.data;r4r6_data_raw4.data];
r4r6_data=r4r6_data_raw.data;
r4r6_slow=r4r6_data(mod(r4r6_data(:,1),2)==1,:);
r4r6_slow_headers=r4r6_data_raw.rowheaders(mod(r4r6_data(:,1),2)==1,:);
r4r6_fast=r4r6_data(mod(r4r6_data(:,1),2)==0,:);
r4r6_fast_headers=r4r6_data_raw.rowheaders(mod(r4r6_data(:,1),2)==0,:);
%delete unfolding step of halo tag
% r4r6_slow=r4r6_slow(r4r6_slow(:,5)>35 & r4r6_slow(:,3)<25,:);
% r4r6_slow_headers=r4r6_slow_headers(r4r6_slow(:,5)>35 & r4r6_slow(:,3)<25,:);
% r4r6_fast=r4r6_fast(r4r6_fast(:,5)>35 & r4r6_fast(:,3)<25,:);
% r4r6_fast_headers=r4r6_fast_headers(r4r6_fast(:,5)>35 & r4r6_fast(:,3)<25,:);

%% identify force cycles that have matching number of transitions
filenames_slow=unique(r4r6_slow_headers);
for i=1:length(filenames_slow)
    file_name_temp_slow=filenames_slow{i};
    data_file_name_temp_slow=r4r6_slow(strcmp(r4r6_slow_headers,file_name_temp_slow),:);
    cycles_no_slow=unique(data_file_name_temp_slow(:,1));
    for j=1:length(cycles_no_slow)
        cycle_no_temp_slow=cycles_no_slow(j);
        data_onecycle_temp_slow=data_file_name_temp_slow(data_file_name_temp_slow(:,1)==cycle_no_temp_slow,:);
        if length(data_onecycle_temp_slow(:,1))==transition_no
            parsed_unfolding_data_slow(end+1,:,1:transition_no)=data_onecycle_temp_slow(1:transition_no,:)';
        end
    end
end


filenames_fast=unique(r4r6_fast_headers);
for i=1:length(filenames_fast)
    file_name_temp_fast=filenames_fast{i};
    data_file_name_temp_fast=r4r6_fast(strcmp(r4r6_fast_headers,file_name_temp_fast),:);
    cycles_no_fast=unique(data_file_name_temp_fast(:,1));
    for j=1:length(cycles_no_fast)
        cycle_no_temp_fast=cycles_no_fast(j);
        data_onecycle_temp_fast=data_file_name_temp_fast(data_file_name_temp_fast(:,1)==cycle_no_temp_fast,:);
        if length(data_onecycle_temp_fast(:,1))==transition_no
            parsed_unfolding_data_fast(end+1,:,1:transition_no)=data_onecycle_temp_fast(1:transition_no,:)';
        end
    end
end

%% find the parameters of individual tansitions

transition_matrix=[0 0;1 0];
optimized_overall=[];

for l=1:no_repeats
    for k=1:transition_no
        r4r6_fast_force_bin=histc(parsed_unfolding_data_fast(:,3,k),0:1:30);
        r4r6_slow_force_bin=histc(parsed_unfolding_data_slow(:,3,k),0:1:30);
        r4r6_fast_hist=[0:1:30; r4r6_fast_force_bin'];
        r4r6_slow_hist=[0:1:30; r4r6_slow_force_bin'];
        optimized_overall(:,:,k,l)=find_unfolding_parameters_main_multistart(transition_matrix,[0.36 3.37],[r4r6_slow_hist' r4r6_fast_hist(2,:)'],1,1:1:2,[0.0005      3],[0.000000000001 0.2],[0.1 20],1,2);
    end
end

save('r9r12_optimized_overall_multistart.mat','optimized_overall');
%save('r4r6_optimized_overall.mat',optimized_overall,'-ascii','-double');

